package com.ruoyi.stock2.thread;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.stock2.domain.*;
import com.ruoyi.stock2.mapper.DownTradedataScheduleMapper;
import com.ruoyi.stock2.service.IDownTradedataFailService;
import com.ruoyi.stock2.service.ITRADEDATA200Service;
import com.ruoyi.stock2.service.ITRADEDATAService;
import com.ruoyi.stock2.thread.aboutDownloadData.UpdateStocksSymbolDataService;
import com.ruoyi.stock2.thread.aboutDownloadFailData.FailCompPingjun3;
import com.ruoyi.stock2.thread.aboutDownloadFailData.FailDownLoadStockTradeData2;
import com.ruoyi.stock2.utils.DateUtils2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;


public class DownLoadFailThread implements Runnable {

	private static final Logger log = LoggerFactory.getLogger(DownLoadFailThread.class);
	boolean working = false;
	ITRADEDATAService itradedataService;
	ITRADEDATA200Service itradedata200Service;
	IDownTradedataFailService iDownTradedataFailService;
	DownTradedataScheduleMapper downLoadTradedataScheduleMapper;
	UpdateStocksSymbolDataService updateStocksSymbolDataService;

	//单例实现 涉及到任务状态
	public static DownLoadFailThread getInstance() {
		return Singtonle.instance;
	}

	private static class Singtonle {
		private static DownLoadFailThread instance = new DownLoadFailThread();
	}

	@Override
	public void run() {
		working = true;
		itradedata200Service = (ITRADEDATA200Service) SpringUtils.getBean(ITRADEDATA200Service.class);
		itradedataService = (ITRADEDATAService) SpringUtils.getBean(ITRADEDATAService.class);
		downLoadTradedataScheduleMapper = (DownTradedataScheduleMapper)SpringUtils.getBean(DownTradedataScheduleMapper.class);
		updateStocksSymbolDataService = (UpdateStocksSymbolDataService)SpringUtils.getBean(UpdateStocksSymbolDataService.class);
		iDownTradedataFailService = (IDownTradedataFailService)SpringUtils.getBean(IDownTradedataFailService.class);


		//下载数据
		FailDownLoadStockTradeData2.getInstance().run();
		log.warn("完成下载");
		//补全平均值
		FailCompPingjun3 cpjt = FailCompPingjun3.getInstance();
		cpjt.run();
		log.warn("完成平均");

		long a = System.currentTimeMillis();
		//获取系统已经完成下载数据日期
		List<DownTradedataFail> failsstocks = iDownTradedataFailService.selectDownTradedataFailList(null);//from stock order by SYMBOL ASC
		for (DownTradedataFail stock : failsstocks) {
			TRADEDATA tradeDataKey = new TRADEDATA(stock.getSymbol(),
					DateUtils2.formatStringToDate(stock.getFromdate(), "yyyyMMdd"));
			List<TRADEDATA> list = itradedataService.get200ByKeyDateTimeDesc(tradeDataKey);
			List<TRADEDATA200> list200 = new ArrayList<TRADEDATA200>(30);
			int sizec = list.size()-1;
			for (int i = sizec; i >= 0; i--) {
				TRADEDATA200 tradedata200 = new TRADEDATA200();
				tradedata200.setSymbol(list.get(i).getSymbol());
				tradedata200.setDatetime(list.get(i).getDatetime());
				List<TRADEDATA200> list200s = itradedata200Service.selectTRADEDATA200List(tradedata200);
				if(list200s.size()==0){
					BeanUtils.copyProperties(list.get(i), tradedata200);
					list200.add(tradedata200);
				}else{
					BeanUtils.copyProperties(list.get(i), tradedata200);
					itradedata200Service.updateTRADEDATA200(tradedata200);
				}

				if(list200.size()==30){
					itradedata200Service.insertTRADEDATA200b(list200);
					list200.clear();
				}
			}
			if(list200.size()>0){
				itradedata200Service.insertTRADEDATA200b(list200);
				list200.clear();
			}

		}
		long c = System.currentTimeMillis();
		long l2 = (c-a)/1000;
		log.warn("写入tradedata200完毕耗时"+l2+" 秒");

		working = false;
	}


	public boolean isWorking() {
		return working;
	}

	public void setWorking(boolean working) {
		this.working = working;
	}


}
